using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XGame.UI;
using XGame.Entity;

namespace XGame
{
    public class MainGame : MonoBehaviour
    {
        public abstract class GameStateBase
        {
            public abstract void OnEnter();
            public virtual bool OnTick(float delta){ return true; }//return true => still alive; false => to next status
            public abstract GameStateBase OnEnd();  // next status
        }

        public class GameStateBegin : GameStateBase
        {
            public override void OnEnter()
            {
                // init all manager
                UIManager.GetInstance();
                EntityManager.GetInstance();

                Debug.Log("GameStateBegin OnEnter");
            }

            public override bool OnTick(float delta)
            {
                UIManager.GetInstance().CreateUIRootNode();
                Debug.Log("GameStateBegin OnTick");

                return true;
            }

            public override GameStateBase OnEnd()
            {
                //TODO next status
                Debug.Log("GameStateBegin OnEnd");
                return new GameStateLoadingResource();
            }
        }



        public class GameStateLoadingResource : GameStateBase
        {
            public override void OnEnter()
            {
                Debug.Log("GameStateLoadingResource OnEnter");
            }

            public override bool OnTick(float delta)
            {
                Debug.Log("GameStateLoadingResource OnTick");

                return true;
            }

            public override GameStateBase OnEnd()
            {
                //TODO next status
                Debug.Log("GameStateLoadingResource OnEnd");
                
                return new GameStateGame();
            }       
        }

        public class GameStateGame : GameStateBase
        {
            public override void OnEnter()
            {
                Debug.Log("GameStateGame OnEnter");

            }

            public override bool OnTick(float delta)
            {

                Debug.Log("GameStateGame Tick");
                return false;
            }

            public override GameStateBase OnEnd()
            {
                //TODO next status
                return null;
            }
        }

        private static MainGame m_Instance;

        public GameStateBase m_GameState;


        public static MainGame GetInstance()
        {
            if(m_Instance == null)
            {
                Debug.LogError("error!!!!");
                return m_Instance;
            }
            return m_Instance;
        }


        void Awake() 
        {
            this.name = "MainGame";
            m_Instance = this;
            m_GameState = new GameStateBegin();
            m_GameState.OnEnter();
            DontDestroyOnLoad(this.gameObject);
        }


        // Update is called once per frame
        void Update()
        {
            if(m_GameState.OnTick(Time.deltaTime))
            {
                m_GameState = m_GameState.OnEnd();
                m_GameState.OnEnter();
            }
        }
    }
}

